package com.it2297.controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.alee.extended.image.WebDecoratedImage;
import com.alee.extended.image.WebImage;
import com.alee.laf.text.WebTextField;
import com.it2297.component.AbstractPagination;
import com.it2297.component.ImageChooser;
import com.it2297.component.ImageCropper;
import com.it2297.main.App;
import com.it2297.model.Bid;
import com.it2297.model.UsedItem;
import com.it2297.model.UsedItem.ItemCondition;
import com.it2297.model.UsedItem.SortBy;
import com.it2297.model.UsedItem.Status;
import com.it2297.model.UsedItem.Type;
import com.it2297.model.User;
import com.it2297.util.ImageProcessor;
import com.it2297.view.UsedItemBrowseView;
import com.it2297.view.UsedItemCreateUpdateDialog;
import com.it2297.view.UsedItemCreateUpdateDialog.DialogType;
import com.it2297.view.UsedItemDetailDialog;
import com.it2297.view.UsedItemSearchDialog;

public class UsedItemController
{
	private UsedItemBrowseView browseView;
	private UsedItemCreateUpdateDialog createUpdateDialog;
	private UsedItemDetailDialog detailDialog;
	private UsedItemSearchDialog searchDialog;
	private final ArrayList<BufferedImage> cachedImages = new ArrayList<BufferedImage>();
	private final ArrayList<BufferedImage> loadedImages = new ArrayList<BufferedImage>();
	private UsedItem selectedItem;
	private Object[] selectedItems;

	public UsedItemController(UsedItemBrowseView browseView, UsedItemCreateUpdateDialog createUpdateDialog, UsedItemDetailDialog detailDialog, UsedItemSearchDialog searchDialog)
	{
		this.browseView = browseView;
		this.createUpdateDialog = createUpdateDialog;
		this.detailDialog = detailDialog;
		this.searchDialog = searchDialog;

		browseView.addFilterItems(new Object[] { "NONE" });
		browseView.addFilterItems(UsedItem.Type.values());
		browseView.addStatusItem(UsedItem.Status.values());
		browseView.addTableMouseListener(new BrowseViewTableMouseListener());
		browseView.addCreateActionListener(new BrowseViewCreateActionListener());
		browseView.addUpateActionListener(new BrowseViewUpdateActionListener());
		browseView.addDeleteActionListener(new BrowseViewDeleteActionListener());
		browseView.addSearchActionListener(new BrowseViewSearchActionListener());
		browseView.addStatusActionListener(new BrowseViewStatusActionListener());
		browseView.addSortingActionListener(new BrowseViewSortingActionListener());
		browseView.addFilteringActionListener(new BrowseViewFilteringActionListener());
		browseView.addResetActionListener(new BrowseViewResetActionListener());
		browseView.setPagination(new BrowseViewPagination());

		createUpdateDialog.setComboBoxApplicationStatus(UsedItem.Status.values());
		createUpdateDialog.setComboBoxItemConditions(getItemConditions());
		createUpdateDialog.setComboBoxTypes(UsedItem.Type.values());
		createUpdateDialog.addDonorNRICFocusListener(new CreateUpdateDialogDonorNRICFocusListener());
		createUpdateDialog.addUploadActionListener(new CreateUpdateDialogUploadActionListener());
		createUpdateDialog.addCancelActionListener(new CreateUpdateDialogCancelActionListener());
		createUpdateDialog.addPreviousActionListener(new CreateUpdateDialogPreviousActionListener());
		createUpdateDialog.addNexActionListener(new CreateUpdateDialogNextActionListener());
		createUpdateDialog.addFinishActionListener(new CreateUpdateDialogFinishActionListener());

		detailDialog.addBidActionListener(new DetailDialogBidActionListener());
		detailDialog.addCloseActionListener(new DetailDialogCloseActionListener());
		detailDialog.setPagination(new DetailDialogPagination());

		searchDialog.addSearchActionListener(new SearchDialogSearchActionListener());
		searchDialog.addCancelActionListener(new SearchDialogCancelActionListener());
	}

	private boolean bid(String comment)
	{
		System.out.println("In bid");

		Bid bid = new Bid();

		bid.setComment(comment);
		bid.setDateBid(new Timestamp(new Date().getTime()));
		bid.setNeedy(App.currentNeedy);
		bid.setUsedItem(selectedItem);

		return bid.create();
	}

	private void clearCachedImages()
	{
		cachedImages.clear();
	}

	private boolean create()
	{

		boolean isSuccessful;
		UsedItem usedItem = new UsedItem();

		usedItem.setUuid(createUpdateDialog.getUuid());
		usedItem.setTitle(createUpdateDialog.getTitle());
		usedItem.setDescription(createUpdateDialog.getDescription());
		usedItem.setType(createUpdateDialog.getItemType());
		usedItem.setStockCount(createUpdateDialog.getQuantity());
		usedItem.setDateCreate(new Timestamp(new Date().getTime()));
		usedItem.setDateUpdate(new Timestamp(new Date().getTime()));
		usedItem.setItemCondition(createUpdateDialog.getItemCondition());
		usedItem.setStatus(createUpdateDialog.getApplicationStatus());
		usedItem.setComment(createUpdateDialog.getComment());
		usedItem.setDonorNRIC(createUpdateDialog.getDonorNRIC());

		isSuccessful = usedItem.create();

		return isSuccessful;
	}

	private boolean delete()
	{
		boolean isSuccessful = false;

		for (Object selectedItem : selectedItems)
		{
			UsedItem _selectedItem = (UsedItem) selectedItem;

			if (_selectedItem.delete())
			{
				isSuccessful = true;
			}
			else
			{
				isSuccessful = false;
				break;
			}
		}

		return isSuccessful;
	}

	private BufferedImage generateThumbnail(BufferedImage bimg)
	{
		bimg = ImageProcessor.resize(bimg, 100, 100, true, false);
		return bimg;
	}

	private ItemCondition[] getItemConditions()
	{
		return UsedItem.ItemCondition.values();
	}

	private int getSceneNum()
	{
		int sceneNum = 0;
		for (Component scene : createUpdateDialog.getScenesPanel().getComponents())
		{
			if (scene.isVisible())
			{
				sceneNum = Integer.parseInt(((JPanel) scene).getName());
			}
		}

		return sceneNum;
	}

	private void loadImages()
	{
		loadedImages.clear();
		int i = 0;
		while (true)
		{
			try
			{
				loadedImages.add(ImageIO.read(new File("img/used-item/" + selectedItem.getUuid() + "-" + i + ".png")));
				i++;
			}
			catch (Exception ex)
			{
				break;
			}
		}
	}

	private void refreshBrowseView()
	{
		Status applicationStatus = browseView.getStatusType();
		SortBy sortType = browseView.getSortType();
		Type filterType = browseView.getItemType();
		String titleKeywords = searchDialog.getTitleKeywords();
		String descriptionKeywords = searchDialog.getDescriptionKeywords();

		browseView.setPaginationCount(10);
		browseView.setPaginationTotal(UsedItem.getRecordNum(applicationStatus, sortType, filterType, titleKeywords, descriptionKeywords));

		browseView.refresh();
	}

	private void saveImages()
	{

		for (int i = 0; i < cachedImages.size(); i++)
		{
			try
			{
				ImageIO.write(cachedImages.get(i), "png", new File("img/used-item/" + createUpdateDialog.getUuid() + "-" + i + ".png"));
				ImageIO.write(generateThumbnail(cachedImages.get(i)), "png", new File("img/used-item/" + createUpdateDialog.getUuid() + "-" + i + "-thumbnail.png"));
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	private boolean showBidDialog()
	{
		String title = detailDialog.getTitle();
		boolean isSuccessful = false;
		String comment = JOptionPane.showInputDialog(null, "Briefly explain to us why do you need this item:", null, JOptionPane.INFORMATION_MESSAGE);

		if (comment != null)
		{
			if (comment.isEmpty())
			{
				JOptionPane.showMessageDialog(null, "You didn't enter anything!");
				return isSuccessful = false;
			}

			int option = JOptionPane.showConfirmDialog(null, "Confirm bidding for item, " + title + "?", "Bid", JOptionPane.YES_NO_OPTION);

			switch (option)
			{
				case JOptionPane.YES_OPTION:
					isSuccessful = bid(comment);
					if (isSuccessful)
					{
						JOptionPane.showMessageDialog(null, "You bidded an item!");
					}
					else
					{
						JOptionPane.showMessageDialog(null, "You have already bidded this item!");
						isSuccessful = false;
					}
					break;
				case JOptionPane.NO_OPTION:
					isSuccessful = false;
					break;
			}
		}
		else
		{
			isSuccessful = false;
		}

		return isSuccessful;

	}

	private void showCreateDialog()
	{
		clearCachedImages();
		createUpdateDialog.reset();
		updateSceneControl();
		createUpdateDialog.setDialogType(DialogType.CREATE);
		createUpdateDialog.setUuid(UUID.randomUUID().toString());
		createUpdateDialog.setVisible(true);
	}

	private boolean showDeleteDialog()
	{
		boolean response = false;
		int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + browseView.getSelectedItems().length + " item(s)?", "Deletion", JOptionPane.YES_NO_OPTION);
		switch (option)
		{
			case JOptionPane.YES_OPTION:
				response = true;
				break;
			case JOptionPane.NO_OPTION:
				response = false;
				break;
		}
		return response;

	}

	private void showDetailDialog()
	{
		detailDialog.setTitle(selectedItem.getTitle());
		detailDialog.setInformation("<html><body style='font-family:Dialog 12;'>" + "<h3>" + selectedItem.getTitle() + "</h3>" + "<p><small>" + selectedItem.getDescription() + "</small></p>" + "<br/><hr/><br/>" + "<small>" + "<strong>Quantity: </strong>" + selectedItem.getStockCount() + "<br/>" + "<strong>Type: </strong>" + selectedItem.getType() + "<br/>" + "<strong>Item Condition: </strong>" + selectedItem.getItemCondition() + "<br/>" + "<strong>Date Posted: </strong>" + selectedItem.getDateCreate() + "<br/>" + "<strong>Donated by: </strong>" + selectedItem.getDonorNRIC() + "<br/>" + "</small>" + "<br/><hr/><br/>" + "<small>" + "<strong>Other Notes: </strong>" + selectedItem.getComment() + "<br/>" + "</small>" + "</body></html>");
		detailDialog.setPaginationTotal(loadedImages.size());
		detailDialog.refresh();

		detailDialog.setVisible(true);
	}

	private void showImageChooser()
	{
		ImageChooser imgChooser = new ImageChooser();
		imgChooser.setMultiSelectionEnabled(true);
		imgChooser.showDialog();

		BufferedImage[] bimgs = imgChooser.getSelectedImages();

		stageImages(bimgs);
	}

	private void showUpdateDialog()
	{
		createUpdateDialog.setDialogType(DialogType.UPDATE);

		createUpdateDialog.setId(selectedItem.getId());
		createUpdateDialog.setUuid(selectedItem.getUuid());
		createUpdateDialog.setTitle(selectedItem.getTitle());
		createUpdateDialog.setDescription(selectedItem.getDescription());
		createUpdateDialog.setQuantity(selectedItem.getStockCount());
		createUpdateDialog.setItemType(selectedItem.getType());
		createUpdateDialog.setItemCondition(selectedItem.getItemCondition());
		createUpdateDialog.setApplicationStatus(selectedItem.getStatus());
		createUpdateDialog.setDonorNRIC(selectedItem.getDonorNRIC());
		createUpdateDialog.setComment(selectedItem.getComment());

		loadImages();
		stageImages(loadedImages.toArray(new BufferedImage[] {}));

		createUpdateDialog.setVisible(true);

	}

	private void stageImages(BufferedImage[] bimgs)
	{
		if (bimgs != null)
		{
			for (final BufferedImage bimg : bimgs)
			{
				// cache original resolution
				cachedImages.add(bimg);

				BufferedImage thumbnail = generateThumbnail(bimg);

				final WebDecoratedImage wdimg = new WebDecoratedImage(new ImageIcon(thumbnail));
				wdimg.setRound(0);
				wdimg.addMouseListener(new MouseAdapter()
				{
					private int i = -1;

					@Override
					public void mouseClicked(MouseEvent e)
					{
						if (e.getClickCount() > 1)
						{
							if (i < 0)
							{
								i = cachedImages.indexOf(bimg);
							}

							ImageCropper dt = new ImageCropper(cachedImages.get(i));
							dt.setVisible(true);

							BufferedImage thumbnail = generateThumbnail(dt.getResultImage());
							wdimg.setImage(thumbnail);

							cachedImages.set(i, dt.getResultImage());

							createUpdateDialog.getPreviewPanel().revalidate();
							createUpdateDialog.getPreviewPanel().repaint();

						}
					}
				});

				createUpdateDialog.getPreviewPanel().add(wdimg);
				createUpdateDialog.getPreviewPanel().revalidate();
				createUpdateDialog.getPreviewPanel().repaint();

			}
		}
		else
		{
			System.out.println("nothing");
		}
	}

	private boolean update()
	{
		boolean isSuccessful;
		UsedItem usedItem = new UsedItem();

		usedItem.setId(createUpdateDialog.getId());
		usedItem.setUuid(createUpdateDialog.getUuid());
		usedItem.setTitle(createUpdateDialog.getTitle());
		usedItem.setDescription(createUpdateDialog.getDescription());
		usedItem.setType(createUpdateDialog.getItemType());
		usedItem.setStockCount(createUpdateDialog.getQuantity());
		usedItem.setDateUpdate(new Timestamp(new Date().getTime()));
		usedItem.setItemCondition(createUpdateDialog.getItemCondition());
		usedItem.setStatus(createUpdateDialog.getApplicationStatus());
		usedItem.setComment(createUpdateDialog.getComment());
		usedItem.setDonorNRIC(createUpdateDialog.getDonorNRIC());

		isSuccessful = usedItem.update();

		return isSuccessful;
	}

	private void updateSceneControl()
	{
		final JButton btnCancel = createUpdateDialog.getCancelButton();
		final JButton btnPrevious = createUpdateDialog.getPreviousButton();
		final JButton btnNext = createUpdateDialog.getNextButton();
		final JButton btnFinish = createUpdateDialog.getFinishButton();

		final int sceneNum = getSceneNum();

		switch (sceneNum)
		{
			case 1:
				btnCancel.setVisible(true);
				btnPrevious.setVisible(true);
				btnNext.setVisible(true);
				btnFinish.setVisible(false);

				btnCancel.setEnabled(true);
				btnPrevious.setEnabled(false);
				btnNext.setEnabled(true);
				btnFinish.setEnabled(false);
				break;
			case 3:
				btnCancel.setVisible(false);
				btnPrevious.setVisible(false);
				btnNext.setVisible(false);
				btnFinish.setVisible(true);

				btnCancel.setEnabled(false);
				btnPrevious.setEnabled(false);
				btnNext.setEnabled(false);
				btnFinish.setEnabled(true);
				break;
			default:
				btnCancel.setVisible(true);
				btnPrevious.setVisible(true);
				btnNext.setVisible(true);
				btnFinish.setVisible(false);

				btnCancel.setEnabled(true);
				btnPrevious.setEnabled(true);
				btnNext.setEnabled(true);
				btnFinish.setEnabled(false);
				break;
		}
	}

	private boolean validateForm()
	{
		boolean isValid = true;
		String errorMessage = "";

		final String title = createUpdateDialog.getTitle();
		final String description = createUpdateDialog.getDescription();
		// final String type = createDialog.getType();
		// final int quantity = createDialog.getQuantity();
		// final ItemCondition itemCondition = createDialog.getItemCondition();
		final String comment = createUpdateDialog.getComment();
		// final String donorNRIC = createDialog.getDonorNRIC();
		// final Status applicationStatus = createDialog.getApplicationStatus();

		if (title.isEmpty())
		{
			errorMessage += "Title is empty.\n";
			isValid = false;
		}
		if (description.isEmpty())
		{
			errorMessage += "Description is empty.\n";
			isValid = false;
		}
		else if (description.length() >= 250)
		{
			errorMessage += "Description is too long.\n";
			isValid = false;
		}
		if (comment.isEmpty())
		{
			errorMessage += "Comment is empty.\n";
			isValid = false;
		}

		if (!validateUser())
		{
			errorMessage += "Invalid user NRIC.\n";
			isValid = false;
		}

		if (!errorMessage.isEmpty())
		{
			JOptionPane.showMessageDialog(null, errorMessage, "Validation Error", JOptionPane.ERROR_MESSAGE);
		}

		return isValid;
	}

	private boolean validateUser()
	{
		WebTextField txtDonorNRIC = createUpdateDialog.getDonorNRICField();

		txtDonorNRIC.setText(txtDonorNRIC.getText().toUpperCase());
		if (!txtDonorNRIC.getText().isEmpty() && txtDonorNRIC.getText().length() == 9)
		{
			if (User.isUserExist(txtDonorNRIC.getText()))
			{
				txtDonorNRIC.setTrailingComponent(new WebImage(new ImageIcon(UsedItemCreateUpdateDialog.class.getResource("/com/alee/extended/filechooser/icons/ok.png"))));
				return true;
			}
			else
			{
				txtDonorNRIC.setTrailingComponent(new WebImage(new ImageIcon(UsedItemCreateUpdateDialog.class.getResource("/com/alee/extended/filechooser/icons/remove.png"))));
				return false;
			}
		}
		else
		{
			txtDonorNRIC.setText(" ");
			txtDonorNRIC.setTrailingComponent(new WebImage(new ImageIcon(UsedItemCreateUpdateDialog.class.getResource("/com/alee/extended/optionpane/icons/warning.png"))));
			return false;
		}
	}

	private class BrowseViewCreateActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			showCreateDialog();
			refreshBrowseView();
		}
	}

	private class BrowseViewDeleteActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			boolean response = showDeleteDialog();
			if (response)
			{
				delete();
				refreshBrowseView();
			}

		}
	}

	private class BrowseViewFilteringActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			refreshBrowseView();
		}

	}

	@SuppressWarnings("serial")
	private class BrowseViewPagination extends AbstractPagination
	{

		public BrowseViewPagination()
		{
			Status applicationStatus = browseView.getStatusType();
			SortBy sortType = browseView.getSortType();
			Type filterType = browseView.getItemType();
			String titleKeywords = searchDialog.getTitleKeywords();
			String descriptionKeywords = searchDialog.getDescriptionKeywords();

			this.setCount(10);
			this.setTotal(UsedItem.getRecordNum(applicationStatus, sortType, filterType, titleKeywords, descriptionKeywords));
		}

		@Override
		protected void action(Object[] data)
		{
			// clear all row
			browseView.clearTableRow();

			// add new row base on data size
			for (int i = 0; i < data.length; i += 2)
			{
				if (i <= data.length - 2)
				{
					browseView.addTableRow(new Object[] { data[i], data[i + 1] });
				}
				else
				{
					browseView.addTableRow(new Object[] { data[i] });
				}
			}
		}

		@Override
		protected Object[] subData(int position, int count)
		{
			Status applicationStatus = browseView.getStatusType();
			SortBy sortType = browseView.getSortType();
			Type filterType = browseView.getItemType();
			String titleKeywords = searchDialog.getTitleKeywords();
			String descriptionKeywords = searchDialog.getDescriptionKeywords();

			return UsedItem.getMultiple(position, count, applicationStatus, sortType, filterType, titleKeywords, descriptionKeywords).toArray();

		}
	}

	private class BrowseViewResetActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			searchDialog.reset();
			refreshBrowseView();
		}
	}

	private class BrowseViewSearchActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			searchDialog.setVisible(true);
		}

	}

	private class BrowseViewSortingActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			refreshBrowseView();
		}
	}

	private class BrowseViewStatusActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			refreshBrowseView();
		}
	}

	private class BrowseViewTableMouseListener extends MouseAdapter
	{

		@Override
		public void mouseClicked(MouseEvent mouseEvent)
		{
			selectedItem = (UsedItem) browseView.getSelectedItem();
			selectedItems = browseView.getSelectedItems();

			if (mouseEvent.getClickCount() > 1)
			{
				loadImages();
				showDetailDialog();
			}
		}
	}

	private class BrowseViewUpdateActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			showUpdateDialog();
			refreshBrowseView();
		}
	}

	private class CreateUpdateDialogCancelActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			createUpdateDialog.setVisible(false);
		}

	}

	private class CreateUpdateDialogDonorNRICFocusListener extends FocusAdapter
	{

		@Override
		public void focusGained(FocusEvent e)
		{
			createUpdateDialog.clearDonorNRIC();
		}

		@Override
		public void focusLost(FocusEvent e)
		{
			validateUser();
		}
	}

	private class CreateUpdateDialogFinishActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			createUpdateDialog.setVisible(false);
		}
	}

	private class CreateUpdateDialogNextActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{

			final int sceneNum = getSceneNum();

			switch (sceneNum)
			{
				case 1:
					if (validateForm())
					{
						createUpdateDialog.next();
					}
					break;
				case 2:
					switch (createUpdateDialog.getDialogType())
					{
						case CREATE:
							if (create())
							{
								saveImages();
								createUpdateDialog.next();
							}
							break;
						case UPDATE:
							if (update())
							{
								createUpdateDialog.next();
							}
							break;
					}
					break;
				default:
					createUpdateDialog.next();
					break;
			}

			updateSceneControl();

		}
	}

	private class CreateUpdateDialogPreviousActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			createUpdateDialog.previous();
			updateSceneControl();
		}
	}

	private class CreateUpdateDialogUploadActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			showImageChooser();
		}

	}

	private class DetailDialogBidActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			showBidDialog();
		}
	}

	private class DetailDialogCloseActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			detailDialog.setVisible(false);
		}
	}

	@SuppressWarnings("serial")
	private class DetailDialogPagination extends AbstractPagination
	{
		public DetailDialogPagination()
		{
			if (!loadedImages.isEmpty())
			{
				detailDialog.setImage(loadedImages.get(0));
			}

			this.setCount(1);
			this.setTotal(loadedImages.size());
		}

		@Override
		public void action(Object[] data)
		{
			try
			{
				detailDialog.setImage((BufferedImage) data[0]);

			}
			catch (Exception ex)
			{
				// do nothing
			}
		}

		@Override
		public Object[] subData(int position, int count)
		{
			try
			{
				return loadedImages.subList(position, position + count).toArray();
			}
			catch (Exception ex)
			{
				return null;
			}
		}

	}

	private class SearchDialogCancelActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			searchDialog.setVisible(false);
		}
	}

	private class SearchDialogSearchActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			searchDialog.setVisible(false);
			refreshBrowseView();
		}
	}
}
